<template>
	<view class="index_Content">
		<!-- 通知 -->
		<Notification />
		<template v-if="isIndexContent">
			<!-- 2货代 6 运输  identity_type -->
			<!-- <Home ></Home> -->
			<WebView  propsUrl="/pages/home/index"  v-if="identity_type == 2 || identity_type == 6|| identity_type == 8"></WebView>
			<WebView  propsUrl="/pages/app/index" v-else></WebView>
			 <!-- <ShopIndex v-else></ShopIndex> -->
		</template>

		<update v-if="updateIsShow" ref="updateRef" />


	</view>
</template>

<script>
// #ifndef MP-WEIXIN
import TencentCloudChat from '@tencentcloud/chat';
// 引入setPermissions.js文件
import { getPermissions } from '@/common/getPermissions.js';
import { init, login } from '@/utils/im';
import permision from "@/js_sdk/wa-permission/permission.js"
// #endif



import { HTTP_REQUEST_URL, IMAGE_URL, HTTP_IMG_UTL } from '@/config/app';
import { isBase64, time, createC2CConversation, getCapsule, createVoiceCall } from "@/common/public";
import Home from './home/index.vue'

import update from '../update.vue'

import ShopIndex from "./ShopIndex/index"

import WebView from "@/pages/zWebView/zWebView"



export default {
	components: {
		Home, 
		update,
		ShopIndex,
		WebView
	},
	data() {
		return {
			lang: uni.getStorageSync("lang"),
			plat: '',
			platform: '',
			version: "",
			updateIsShow: false,

			user_id: '',
			url: '',
			identity_type: '',
			current_user_id: uni.getStorageSync("user_id"),
			isIndexContent: false, // 避免页面闪动
		}
	},
	onLoad() {
		var that = this

		// #ifdef APP-PLUS
		login();
		plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
			uni.setStorage({
				key: 'wgtinfoVersionCode',
				data: wgtinfo.version
			})
			this.version = wgtinfo.version
		})

		//检测更新
		let plat = uni.getStorageSync('plat')

		that.plat = plat
		const systemInfo = uni.getSystemInfoSync();
		const platform = systemInfo.platform;
		that.platform = platform
		if (plat == 'app') {
			getPermissions();
			if (that.platform == 'ios') {
				that.requestIosPermission2('record')
			} else {
				that.requestAndroidPermission('record')
				that.xuanfuchuang()
			}
		} else {
			uni.getSystemInfo({
				success: res => {
					if (res.uniPlatform === 'app') {
						that.plat = 'app'
					} else if (res.uniPlatform === 'web') {
						that.plat = 'web'
					} else if (res.uniPlatform === 'mp-weixin') {
						that.plat = 'mp-weixin'
					}
					uni.setStorage({
						key: 'plat',
						data: that.plat
					})

					const systemInfo = uni.getSystemInfoSync();
					const platform = systemInfo.platform;
					that.platform = platform
					if (that.plat == 'app') {
						getPermissions();
						if (platform == 'ios') {
							that.requestIosPermission2('record')
						} else {
							that.requestAndroidPermission('record')
							that.xuanfuchuang()
						}
					}
				}
			})
		}
		// #endif
	},
	onShow() {
		this.getUserInfo()
		this.getJuse();
		// 获取语言
		this.getLang()
		this.getC2cUnreadMsgNum()

		this.mobile = uni.getStorageSync("mobile")
		this.lang = uni.getStorageSync("lang")

		this.setTabbarName()

		this.userInfoFull = uni.getStorageSync("userInfoFull")

		this.$store.commit("getC2cUnreadMsgNum")

		this.updateIsShow = false;
		var that = this
		setTimeout(() => {
			that.updateIsShow = true;
		}, 100)

		// 判断是否选择语言
		const lang = uni.getStorageSync('lang')
		if (!lang) {
			this.$u.route({
				url: '/pages/tabbar/startPage/language'
			})
			return false
		}

		const startPage = uni.getStorageSync('startPage');
		if (!startPage) {
			this.$u.route({
				url: '/pages/tabbar/startPage/startPage',
				type: 'reLaunch'
			})
			return false
		}

		const step = 1000 * 60 * 60 * 24;
		if (startPage && new Date().getTime() - startPage > step) {
			this.$u.route({
				url: '/pages/tabbar/startPage/startPage',
				type: 'reLaunch'
			})
			return false
		}


	},
	methods: {
		//获取名片
		getJuse() {
			if (!this.current_user_id) {
				this.isIndexContent = true;
				return;
			}
			uni.showLoading({
				title: this.$t("正在加载")
			})
			this.$u.api.user.getUserHomepage({
				user_id: this.current_user_id,
				type: 1,
				from: ''
			}).then(res => {
				this.identity_type = res.userInfo.identity_type;
				this.isIndexContent = true;
				uni.hideLoading()
			}).catch((err)=> {
				console.log(err,'hhjkkkk')
				this.isIndexContent = true;
			})
		},
		async getUserInfo() {
			var userinfo = "";
			const {
				deviceBrand,
				deviceModel,
				deviceId,
				osName,
				osVersion
			} = uni.getSystemInfoSync();
			userinfo = deviceBrand + ',' + deviceModel + ',' + deviceId + ',' + osName + ',' + osVersion
			let user = await this.$u.api.user.getUserInfo({
				userinfo: userinfo
			});
			this.userInfoFull = user;
			console.log('this.userInfoFull:', this.userInfoFull);
			uni.setStorageSync("userInfoFull", user)
		},


		// #ifdef APP-PLUS
		//悬浮窗权限检测
		xuanfuchuang() {
			var that = this
			let plat = uni.getStorageSync('plat')
			that.plat = plat
			const systemInfo = uni.getSystemInfoSync();
			const platform = systemInfo.platform;
			that.platform = platform
			uni.setStorageSync('platform', platform)
			if (plat == 'app') {

				if (that.platform != 'ios') {
					let main = plus.android.runtimeMainActivity();
					let pkName = main.getPackageName()
					let Settings = plus.android.importClass('android.provider.Settings')
					let Uri = plus.android.importClass('android.net.Uri')
					let Intent = plus.android.importClass('android.content.Intent')
					let intent = new Intent(
						'android.settings.action.MANAGE_OVERLAY_PERMISSION',
						Uri.parse('package:' + pkName)
					)
					// 检测悬浮窗  
					if (!Settings.canDrawOverlays(main)) {
						uni.setStorageSync('xuanfuchuang_permission', 0);
					} else {
						uni.setStorageSync('xuanfuchuang_permission', 1);
					}
				}
			}
		},
		// #endif


		//安卓麦克风权限
		async requestAndroidPermission(id) {
			var that = this
			var result = await permision.requestAndroidPermission(id)
			if (result == 1) {
				uni.setStorageSync('record_permission', 1);
			} else if (result == 0) {
				uni.setStorageSync('record_permission', 0);
			} else {
				//uni.setStorageSync('record_permission', 1);
			}
		},

		//ios麦克风权限
		async requestIosPermission2(id) {
			var that = this
			var result = await permision.judgeIosPermission(id)
			if (result == false) {
				uni.setStorageSync('record_permission', 0);
			} else {
				uni.setStorageSync('record_permission', 1);
			}
		},

		setTabbarName() {
			if (uni.getStorageSync("lang") != 'ru') {
				uni.setTabBarItem({
					index: 0, // tabIndex，tabbar的哪一项，从0开始
					text: '首页', // 新的名称
				});
				uni.setTabBarItem({
					index: 1, // tabIndex，tabbar的哪一项，从0开始
					text: '工作台', // 新的名称
				});
				uni.setTabBarItem({
					index: 2, // tabIndex，tabbar的哪一项，从0开始
					text: '消息', // 新的名称
				});
				uni.setTabBarItem({
					index: 3, // tabIndex，tabbar的哪一项，从0开始
					text: '我的', // 新的名称
				});
			} else {
				uni.setTabBarItem({
					index: 0, // tabIndex，tabbar的哪一项，从0开始
					text: 'Главная', // 新的名称
				});
				uni.setTabBarItem({
					index: 1, // tabIndex，tabbar的哪一项，从0开始
					text: 'Кабинет', // 新的名称
				});
				uni.setTabBarItem({
					index: 2, // tabIndex，tabbar的哪一项，从0开始
					text: 'Сообщения', // 新的名称
				});
				uni.setTabBarItem({
					index: 3, // tabIndex，tabbar的哪一项，从0开始
					text: 'Мой', // 新的名称
				});
			}
		},




		// tabbar 在原有tabbar/index  页面赋值过来的
		getC2cUnreadMsgNum() {
			const that = this
			const mobile = uni.getStorageSync('mobile')
			that.isShow = 1
			that.$u.api.message.getC2cUnreadMsgNum({
				mobile: mobile,
			}).then(res => {
				that.$u.api.basic.getNoticeNum().then(r => {
					that.listLang = [{
						iconPath: "/static/images/tabbar/newStyle/t1.png",
						selectedIconPath: "/static/images/tabbar/t1s.png",
						text: that.$t('首页'),
						// count: 2,
						// isDot: true,
						customIcon: false,
						pagePath: '/pages/tabbar/index'
					},
					{
						iconPath: "/static/images/tabbar/newStyle/t2.png",
						selectedIconPath: "/static/images/tabbar/t2s.png",
						text: that.$t('订单'),
						customIcon: false,
						// isDot: true,
						pagePath: '/pages/tabbar/order/index'
					},
					{
						iconPath: "/static/images/tabbar/newStyle/t4.png",
						selectedIconPath: "/static/images/tabbar/t4s.png",
						text: that.$t('消息'),
						count: Number(res.AllC2CUnreadMsgNum) + Number(r.other) + Number(r
							.system),
						customIcon: false,
						pagePath: '/TUIKit/components/TUIConversation/list'
					},
					{
						iconPath: "/static/images/tabbar/newStyle/t5.png",
						selectedIconPath: "/static/images/tabbar/t5s.png",
						text: that.$t('我的'),
						// count: 23,
						// isDot: false,
						customIcon: false,
						pagePath: '/pages/tabbar/user/index'
					},
					]
					that.isShow = 0
				})
			})
		},


		// 获取语言
		getLang() {
			// 获取缓存lang当前语言值
			uni.getStorage({
				key: 'lang',
				success: res => {
					if (res.data == 'zh') { //zh为中国，赋值0
						this.lang = 'zh'
					} else { //其他为俄罗斯，赋值1
						this.lang = 'ru'
					}
				}
			});
		},




	}

}
</script>

<style scoped></style>